package utils;

import bean.Component;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.xml.XmlDocument;
import ui.ColumnDialog;
import ui.ConfigDialog;

import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Objects;

public class AndroidUtils {

    /**
     * tchat组件的所有配置信息
     */
    private static final ArrayList<Component> COMPONENTS = new ArrayList<>();
    private static final ArrayList<Component> TCHATS_COMPONENTS = new ArrayList<>();
    private static final ArrayList<Component> MPAAS_COMPONENTS = new ArrayList<>();
    private static final ArrayList<Component> TMF_COMPONENTS = new ArrayList<>();
    private static final boolean FALSE = false;

    /**
     * 组件分组
     *
     * */

    /**
     * AI组件
     * SCP_AiPlugin
     * SCP_AiUI
     */

    private static final String AI = "AI";

    /**
     * mPaas适配
     * SCP_MpaasAdapter
     * SCP_MpaasBrowser
     * SCP_MpaasUiLib
     */

    private static final String MPAAS_ADAPTER = "mPaas适配";


    /**
     * tmf APP 适配层组件和浏览器
     * SCP_TMFBrowser
     * SCP_WebViewTMFKit
     * SCP_TmfAdapter
     */
    private static final String TMF_ADAPTER = "Tmf适配";


    /**
     * 密码器
     * UKEY
     * SCP_DssPlugin
     * SCP_DssUI
     */

    private static final String UKEY = "UKEY";

    /**
     * 联系人
     * SCP_ContactPlugin
     * SCP_ContactUI
     */
    private static final String CONTACT = "联系人";

    /**
     * 浏览器
     * SCP_Browser
     * SCP_TBrowser
     */
    private static final String BROWSER = "浏览器";

    /**
     * 基础
     * SCP_BaseUI
     * SCP_PickerView
     * SCP_UiLib
     */
    private static final String BASE = "基础";

    /**
     * 商旅
     * SCP_TravelPlugin
     * SCP_TravelUI
     * SCP_AlipayPlugin
     * SCP_WechatPayPlugin
     */
    private static final String BUSINESS = "商旅";

    /**
     * 智能穿戴
     * SCP_SmartWearPlugin
     * SCP_SmartWearUI
     */
    private static final String SMART_WEAR = "智能穿戴";

    /**
     * IM组件
     * SCP_ImUI
     */
    private static final String IM = "IM";

    /**
     * VOIP
     * SCP_VoipUI
     */

    private static final String VOIP = "VOIP";

    /**
     * 安全键盘
     * SCP_SKeyboard
     */

    private static final String SAFE_KEYBOARD = "安全键盘";

    /**
     * 广告
     * SCP_AdMediaUI
     */
    private static final String ADMEDIA = "广告";

    /**
     * 红包
     * SCP_RedPacketUI
     */
    private static final String RED_PACKET = "红包";

    /**
     * 会议
     * SCP_MeetingUI
     */

    private static final String MEETING = "会议";

    /**
     * 日志
     * SCP_LogUI
     */
    private static final String LOG = "日志";

    /**
     * 扫码
     * SCP_QrCodeUI
     */

    private static final String QRCODE = "扫码";

    /**
     * 未分组
     * SCP_MainUI
     */
    private static final String MAIN = "主UI";

    /**
     * 下载
     * SCP_DownloadUI
     */
    private static final String DOWNLOAD = "下载";

    /**
     * 新闻
     * SCP_NewsUI
     */
    private static final String NEW = "新闻";

    /**
     * 用户
     * SCP_UserUI
     */
    private static final String USER = "用户";

    /**
     * 云盘
     * SCP_TPanUI
     */

    private static final String TPAN = "云盘";

    /**
     * 虚拟服务器组价
     * SCP_VirtualServer
     */

    private static final String VIRTUAL_SERVER = "虚拟服务器";

    static {
        /**
         * 广告UI组件
         * */
        Component admediaUI = new Component();
        admediaUI.setName("SCP_AdMediaUI");
        admediaUI.setAttribute("广告UI");
        admediaUI.setType("功能组件");
        admediaUI.setEnable(FALSE);
        admediaUI.setGroup(ADMEDIA);
        COMPONENTS.add(admediaUI);

        /**
         * AI插件
         * */
        Component AiPlugin = new Component();
        AiPlugin.setName("SCP_AiPlugin");
        AiPlugin.setAttribute("AI插件和AIUI");
        AiPlugin.setType("功能组件");
        AiPlugin.setEnable(FALSE);
        AiPlugin.setGroup(AI);
        COMPONENTS.add(AiPlugin);


        /**
         * 虚拟服务器组件
         * */
        Component VirtualServer = new Component();
        VirtualServer.setName("SCP_VirtualServer");
        VirtualServer.setAttribute("虚拟服务器");
        VirtualServer.setType("功能组件");
        VirtualServer.setEnable(FALSE);
        VirtualServer.setGroup(VIRTUAL_SERVER);
        COMPONENTS.add(VirtualServer);

        /**
         * AIUI
         * */
        Component AiUI = new Component();
        AiUI.setName("SCP_AiUI");
        AiUI.setAttribute("AI插件和AIUI");
        AiUI.setType("功能组件");
        AiUI.setEnable(FALSE);
        AiUI.setGroup(AI);
        COMPONENTS.add(AiUI);

        /**
         * 支付宝支付插件
         * */
        Component AlipayPlugin = new Component();
        AlipayPlugin.setName("SCP_AlipayPlugin");
        AlipayPlugin.setAttribute("商旅组件集");
        AlipayPlugin.setType("功能组件");
        AlipayPlugin.setEnable(FALSE);
        AlipayPlugin.setGroup(BUSINESS);
        COMPONENTS.add(AlipayPlugin);

        /**
         * 密码器插件
         * */
        Component DssPlugin = new Component();
        DssPlugin.setName("SCP_DssPlugin");
        DssPlugin.setAttribute("密码器插件和UI");
        DssPlugin.setType("功能组件");
        DssPlugin.setEnable(FALSE);
        DssPlugin.setGroup(UKEY);
        COMPONENTS.add(DssPlugin);

        /**
         * 密码器UI
         * */
        Component DssUI = new Component();
        DssUI.setName("SCP_DssUI");
        DssUI.setAttribute("密码器插件和UI");
        DssUI.setType("功能组件");
        DssUI.setEnable(FALSE);
        DssUI.setGroup(UKEY);
        COMPONENTS.add(DssUI);

        /**
         * 会议UI
         * */
        Component MeetingUI = new Component();
        MeetingUI.setName("SCP_MeetingUI");
        MeetingUI.setAttribute("会议UI");
        MeetingUI.setType("功能组件");
        MeetingUI.setEnable(FALSE);
        MeetingUI.setGroup(MEETING);
        COMPONENTS.add(MeetingUI);

        /**
         * 一点资讯UI
         * */
        Component NewsUI = new Component();
        NewsUI.setName("SCP_NewsUI");
        NewsUI.setAttribute("一点资讯UI");
        NewsUI.setType("功能组件");
        NewsUI.setEnable(FALSE);
        NewsUI.setGroup(NEW);
        COMPONENTS.add(NewsUI);

        /**
         * 扫码UI
         * */
        Component QrCodeUI = new Component();
        QrCodeUI.setName("SCP_QrCodeUI");
        QrCodeUI.setAttribute("扫码UI");
        QrCodeUI.setType("功能组件");
        QrCodeUI.setEnable(FALSE);
        QrCodeUI.setGroup(QRCODE);
        COMPONENTS.add(QrCodeUI);

        /**
         * 红包UI
         * */
        Component RedPacketUI = new Component();
        RedPacketUI.setName("SCP_RedPacketUI");
        RedPacketUI.setAttribute("红包UI");
        RedPacketUI.setType("功能组件");
        RedPacketUI.setEnable(FALSE);
        RedPacketUI.setGroup(RED_PACKET);
        COMPONENTS.add(RedPacketUI);

        /**
         * 智能手环插件
         * */
        Component SmartWearPlugin = new Component();
        SmartWearPlugin.setName("SCP_SmartWearPlugin");
        SmartWearPlugin.setAttribute("智能手环插件和UI");
        SmartWearPlugin.setType("功能组件");
        SmartWearPlugin.setEnable(FALSE);
        SmartWearPlugin.setGroup(SMART_WEAR);
        COMPONENTS.add(SmartWearPlugin);

        /**
         * 智能手环UI
         * */
        Component SmartWearUI = new Component();
        SmartWearUI.setName("SCP_SmartWearUI");
        SmartWearUI.setAttribute("智能手环插件和UI");
        SmartWearUI.setType("功能组件");
        SmartWearUI.setEnable(FALSE);
        SmartWearUI.setGroup(SMART_WEAR);
        COMPONENTS.add(SmartWearUI);

        /**
         * 商旅支付插件
         * */
        Component TravelPlugin = new Component();
        TravelPlugin.setName("SCP_TravelPlugin");
        TravelPlugin.setAttribute("商旅组件集");
        TravelPlugin.setType("功能组件");
        TravelPlugin.setEnable(FALSE);
        TravelPlugin.setGroup(BUSINESS);
        COMPONENTS.add(TravelPlugin);

        /**
         * 商旅支付UI
         * */
        Component TravelUI = new Component();
        TravelUI.setName("SCP_TravelUI");
        TravelUI.setAttribute("商旅组件集");
        TravelUI.setType("功能组件");
        TravelUI.setEnable(FALSE);
        TravelUI.setGroup(BUSINESS);
        COMPONENTS.add(TravelUI);

        /**
         * T盘UI
         * */
        Component TPanUI = new Component();
        TPanUI.setName("SCP_TPanUI");
        TPanUI.setAttribute("T盘UI");
        TPanUI.setType("功能组件");
        TPanUI.setEnable(FALSE);
        TPanUI.setGroup(TPAN);
        COMPONENTS.add(TPanUI);

        /**
         * VOIP
         * */
        Component VoipUI = new Component();
        VoipUI.setName("SCP_VoipUI");
        VoipUI.setAttribute("视频通话UI");
        VoipUI.setType("功能组件");
        VoipUI.setEnable(FALSE);
        VoipUI.setGroup(VOIP);
        COMPONENTS.add(VoipUI);

        /**
         * 微信支付插件
         * */
        Component WechatPayPlugin = new Component();
        WechatPayPlugin.setName("SCP_WechatPayPlugin");
        WechatPayPlugin.setAttribute("商旅组件集");
        WechatPayPlugin.setType("功能组件");
        WechatPayPlugin.setEnable(FALSE);
        WechatPayPlugin.setGroup(BUSINESS);
        COMPONENTS.add(WechatPayPlugin);

        /**
         * 联系人插件
         * */
        Component ContactPlugin = new Component();
        ContactPlugin.setName("SCP_ContactPlugin");
        ContactPlugin.setAttribute("联系人插件和UI");
        ContactPlugin.setType("核心组件");
        ContactPlugin.setEnable(FALSE);
        ContactPlugin.setGroup(CONTACT);
        COMPONENTS.add(ContactPlugin);

        /**
         * 浏览器UI
         * */
        Component PickerView = new Component();
        PickerView.setName("SCP_PickerView");
        PickerView.setAttribute("基础组件集");
        PickerView.setType("核心组件");
        PickerView.setEnable(FALSE);
        PickerView.setGroup(BASE);
        COMPONENTS.add(PickerView);

        /**
         * 安全键盘
         * */
        Component SKeyboard = new Component();
        SKeyboard.setName("SCP_SKeyboard");
        SKeyboard.setAttribute("安全键盘");
        SKeyboard.setType("核心组件");
        SKeyboard.setEnable(FALSE);
        SKeyboard.setGroup(SAFE_KEYBOARD);
        COMPONENTS.add(SKeyboard);

        /**
         * 浏览器基础资源库
         * */
        Component UiLib = new Component();
        UiLib.setName("SCP_UiLib");
        UiLib.setAttribute("基础组件集");
        UiLib.setType("核心组件");
        UiLib.setEnable(FALSE);
        UiLib.setGroup(BASE);
        COMPONENTS.add(UiLib);

        /**
         * 基础UI
         * */
        Component BaseUI = new Component();
        BaseUI.setName("SCP_BaseUI");
        BaseUI.setAttribute("基础组件集");
        BaseUI.setType("核心UI");
        BaseUI.setEnable(FALSE);
        BaseUI.setGroup(BASE);
        COMPONENTS.add(BaseUI);

        /**
         * 联系人UI
         * */
        Component ContactUI = new Component();
        ContactUI.setName("SCP_ContactUI");
        ContactUI.setAttribute("联系人插件和UI");
        ContactUI.setType("核心UI");
        ContactUI.setEnable(FALSE);
        ContactUI.setGroup(CONTACT);
        COMPONENTS.add(ContactUI);

        /**
         * 即时通讯UI
         * */
        Component ImUI = new Component();
        ImUI.setName("SCP_ImUI");
        ImUI.setAttribute("即时通讯UI");
        ImUI.setType("核心UI");
        ImUI.setEnable(FALSE);
        ImUI.setGroup(IM);
        COMPONENTS.add(ImUI);

        /**
         * 日志UI
         * */
        Component LogUI = new Component();
        LogUI.setName("SCP_LogUI");
        LogUI.setAttribute("日志UI");
        LogUI.setType("核心UI");
        LogUI.setEnable(FALSE);
        LogUI.setGroup(LOG);
        COMPONENTS.add(LogUI);

        /**
         * 主组件UI
         * */
        Component MainUI = new Component();
        MainUI.setName("SCP_MainUI");
        MainUI.setAttribute("主组件UI");
        MainUI.setType("核心UI");
        MainUI.setEnable(FALSE);
        MainUI.setGroup(MAIN);
        COMPONENTS.add(MainUI);

        /**
         * 用户UI
         * */
        Component UserUI = new Component();
        UserUI.setName("SCP_UserUI");
        UserUI.setAttribute("用户UI");
        UserUI.setType("核心UI");
        UserUI.setEnable(FALSE);
        UserUI.setGroup(USER);
        COMPONENTS.add(UserUI);

        /**
         * 下载UI
         * */
        Component DownloadUI = new Component();
        DownloadUI.setName("SCP_DownloadUI");
        DownloadUI.setAttribute("下载UI");
        DownloadUI.setType("核心UI");
        DownloadUI.setEnable(FALSE);
        DownloadUI.setGroup(DOWNLOAD);
        COMPONENTS.add(DownloadUI);
    }


    //tchat版本浏览器
    static {
        /**
         * T信浏览器
         * */
        Component TBrowser = new Component();
        TBrowser.setName("SCP_TBrowser");
        TBrowser.setAttribute("Tchats浏览器及插件");
        TBrowser.setType("核心组件");
        TBrowser.setEnable(FALSE);
        TBrowser.setGroup(BROWSER);
        TCHATS_COMPONENTS.add(TBrowser);

        /**
         * 浏览器插件管理组件
         * */
        Component Browser = new Component();
        Browser.setName("SCP_Browser");
        Browser.setAttribute("Tchats浏览器及插件");
        Browser.setType("核心组件");
        Browser.setEnable(FALSE);
        Browser.setGroup(BROWSER);
        TCHATS_COMPONENTS.add(Browser);
    }


    //Mpaas版本浏览器和适配层
    static {
        /**
         * mpaas浏览器
         * */
        Component MpaasBrowser = new Component();
        MpaasBrowser.setName("SCP_MpaasBrowser");
        MpaasBrowser.setAttribute("Mpaas浏览器及插件");
        MpaasBrowser.setType("核心组件");
        MpaasBrowser.setEnable(FALSE);
        MpaasBrowser.setGroup(BROWSER);
        MPAAS_COMPONENTS.add(MpaasBrowser);

        /**
         * 浏览器插件管理组件
         * */
        Component Browser = new Component();
        Browser.setName("SCP_Browser");
        Browser.setAttribute("Mpaas浏览器及插件");
        Browser.setType("核心组件");
        Browser.setEnable(FALSE);
        Browser.setGroup(BROWSER);
        MPAAS_COMPONENTS.add(Browser);


        /**
         * mpaas适配器
         * */
        Component MpaasAdapter = new Component();
        MpaasAdapter.setName("SCP_MpaasAdapter");
        MpaasAdapter.setAttribute("mpaas适配器");
        MpaasAdapter.setType("核心组件");
        MpaasAdapter.setEnable(FALSE);
        MpaasAdapter.setGroup(MPAAS_ADAPTER);
        MPAAS_COMPONENTS.add(MpaasAdapter);
    }


    //Tmf 版本浏览器和适配器
    static {
        /**
         * Tmf浏览器
         * */
        Component TmfBrowser = new Component();
        TmfBrowser.setName("SCP_TMFBrowser");
        TmfBrowser.setAttribute("Tmf浏览器及插件");
        TmfBrowser.setType("核心组件");
        TmfBrowser.setEnable(FALSE);
        TmfBrowser.setGroup(BROWSER);
        TMF_COMPONENTS.add(TmfBrowser);

        /**
         * Tmf webView
         *
         * */
        Component Tmfwb = new Component();
        Tmfwb.setName("SCP_WebViewTMFKit");
        Tmfwb.setAttribute("Tmf浏览器及插件");
        Tmfwb.setType("核心组件");
        Tmfwb.setEnable(FALSE);
        Tmfwb.setGroup(BROWSER);
        TMF_COMPONENTS.add(Tmfwb);


        /**
         * 浏览器插件管理组件
         * */
        Component Browser = new Component();
        Browser.setName("SCP_Browser");
        Browser.setAttribute("Tmf浏览器及插件");
        Browser.setType("核心组件");
        Browser.setEnable(FALSE);
        Browser.setGroup(BROWSER);
        TMF_COMPONENTS.add(Browser);

        /**
         * tmf适配器
         * */
        Component tmfAdapter = new Component();
        tmfAdapter.setName("SCP_TmfAdapter");
        tmfAdapter.setAttribute("Tmf适配组件");
        tmfAdapter.setType("核心组件");
        tmfAdapter.setEnable(FALSE);
        tmfAdapter.setGroup(TMF_ADAPTER);
        TMF_COMPONENTS.add(tmfAdapter);
    }

    // 对应有关联性的组件，需要判断这些组件是否都是存在，否则表示为不存在
    public static HashMap<String, Boolean> handlerComponents(ArrayList<Component> components) {
        HashMap<String, Boolean> componentState = new HashMap<>();
        ArrayList<Component> ai = new ArrayList<>();
        ArrayList<Component> ukey = new ArrayList<>();
        ArrayList<Component> contact = new ArrayList<>();
        ArrayList<Component> browser = new ArrayList<>();
        ArrayList<Component> base = new ArrayList<>();
        ArrayList<Component> business = new ArrayList<>();
        ArrayList<Component> smart_wear = new ArrayList<>();
        for (Component component : components) {
//            System.out.println("reset component  " + component.getName());
            if (component.getGroup().equalsIgnoreCase(AI)) {
                ai.add(component);
            } else if (component.getGroup().equalsIgnoreCase(UKEY)) {
                ukey.add(component);
            } else if (component.getGroup().equalsIgnoreCase(CONTACT)) {
                contact.add(component);
            } else if (component.getGroup().equalsIgnoreCase(BROWSER)) {
                browser.add(component);
            } else if (component.getGroup().equalsIgnoreCase(BASE)) {
                base.add(component);
            } else if (component.getGroup().equalsIgnoreCase(BUSINESS)) {
                business.add(component);
            } else if (component.getGroup().equalsIgnoreCase(SMART_WEAR)) {
                smart_wear.add(component);
            }
        }

        for (Component component : ai) {
            if (!component.isEnable()) {
                componentState.put(AI, false);
            }
        }

        for (Component component : ukey) {
            if (!component.isEnable()) {
                componentState.put(UKEY, false);
            }
        }

        for (Component component : contact) {
            if (!component.isEnable()) {
                componentState.put(CONTACT, false);
            }
        }

        for (Component component : browser) {
            if (!component.isEnable()) {
                componentState.put(BROWSER, false);
            }
        }

        for (Component component : base) {
            if (!component.isEnable()) {
                componentState.put(BASE, false);
            }
        }

        for (Component component : business) {
            if (!component.isEnable()) {
                componentState.put(BUSINESS, false);
            }
        }

        for (Component component : smart_wear) {
            if (!component.isEnable()) {
                componentState.put(SMART_WEAR, false);
            }
        }

        return componentState;
    }

    /**
     * 读取本地的文件绝对文件路径
     */
    public static VirtualFile getGeneralCompileSetting(Project project) {
        String path = project.getBasePath() + File.separator +
                "buildConfig" + File.separator +
                "generalCompileSetting.gradle";
        return LocalFileSystem.getInstance().findFileByPath(path);
    }


    /**
     * 读取本地的buildConfig文件夹的绝对文件路径
     */
    public static VirtualFile getBuildConfigPath(Project project) {
        String path = project.getBasePath() + File.separator +
                "buildConfig" + File.separator;
        return LocalFileSystem.getInstance().findFileByPath(path);
    }

    /**
     * 读取本地的asset文件夹下是否有配置的私钥文件
     */
    public static boolean checkFileIsExit(Project project, String fileName) {
        String path = project.getBasePath() + File.separator +
                "mainProject" + File.separator + "src" + File.separator + "main" + File.separator + "assets" + File.separator;
        File file = new File(path);
        if (file.exists() && file.isDirectory()) {
            String[] list = file.list();
            if (list == null) {
                return true;
            }

            for (String name : list) {
                if (fileName.equalsIgnoreCase(name)) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 读取本地的buildConfig文件夹下的配置文件集合
     */
    public static String[] getBuildConfigFiles(VirtualFile path) {
        File file = new File(path.getPath());//定义一个file对象，用来初始化FileReader
        if (file.exists() && file.isDirectory()) {
            String[] list = file.list();
            if (list == null) {
                return null;
            }

            ArrayList<String> tmpStrings = new ArrayList<>();
            for (String fileName : list) {
                if (!fileName.equalsIgnoreCase(FILE_NAME)) {
                    tmpStrings.add(fileName);
                }
            }

            String[] result = new String[tmpStrings.size()];
            for (int i = 0; i < result.length; i++) {
                result[i] = tmpStrings.get(i);
            }
            return result;
        } else {
            return null;
        }
    }

    /**
     * 获取本地组件配置文件信息
     */
    public static ArrayList<Component> getTchatsComponentsList() {
        for (Component component : COMPONENTS) {
            component.setEnable(FALSE);
        }

        for (Component tchatsComponent : TCHATS_COMPONENTS) {
            tchatsComponent.setEnable(FALSE);
        }
        ArrayList<Component> components = new ArrayList<>();
        components.addAll(COMPONENTS);
        components.addAll(TCHATS_COMPONENTS);
        return components;
    }

    /**
     * 获取mpaas版本的本地组件配置文件信息
     */
    public static ArrayList<Component> getMpaasComponentsList() {
        for (Component component : COMPONENTS) {
            component.setEnable(FALSE);
        }

        for (Component mpaasComponent : MPAAS_COMPONENTS) {
            mpaasComponent.setEnable(FALSE);
        }
        ArrayList<Component> components = new ArrayList<>();
        components.addAll(COMPONENTS);
        components.addAll(MPAAS_COMPONENTS);
        return components;
    }


    /**
     * 获取mpaas版本的本地组件配置文件信息
     */
    public static ArrayList<Component> getTmfComponentsList() {
        for (Component component : COMPONENTS) {
            component.setEnable(FALSE);
        }

        for (Component TmfComponent : TMF_COMPONENTS) {
            TmfComponent.setEnable(FALSE);
        }
        ArrayList<Component> components = new ArrayList<>();
        components.addAll(COMPONENTS);
        components.addAll(TMF_COMPONENTS);
        return components;
    }

    /**
     * 将文件中的内容按照过滤条件读取出来
     */
    public static String readFileSting(VirtualFile path) throws IOException {
        File file = new File(path.getPath());//定义一个file对象，用来初始化FileReader
        FileReader reader = new FileReader(file);//定义一个fileReader对象，用来初始化BufferedReader
        BufferedReader bReader = new BufferedReader(reader);//new一个BufferedReader对象，将文件内容读取到缓存
        StringBuilder sb = new StringBuilder();//定义一个字符串缓存，将字符串存放缓存中
        String s = "";

        while ((s = bReader.readLine()) != null) {//逐行读取文件内容，不读取换行符和末尾的空格
            String replace = s.trim();
            String trim = removeAllSpace(replace);
            sb.append(trim).append("\n");//将读取的字符串添加换行符后累加存放在缓存中
        }
        bReader.close();
        return sb.toString();
    }

    private static PsiFile getConfigFile(Project project, String path) {
        VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path);
        if (virtualFile == null) return null;
        return PsiManager.getInstance(project).findFile(virtualFile);
    }

    public static ArrayList<String> readConfigFileSting(Project project, String path) {
        PsiFile configFile = getConfigFile(project, path);
        assert configFile != null;
        String content = configFile.getText();
        String[] split = content.split("\n");
        return new ArrayList<>(Arrays.asList(split));
    }


    //替换一个url中的端口和域名
    public static String replaceHostAndPort(String host, String port, String url) {
        url = url.replace(" ", "");
        if (!url.startsWith("http")) {
            return url;
        }

        String url_bak = "";
        if (url.contains("//")) {
            String[] splitTemp = url.split("//");
            url_bak = splitTemp[0] + "//";
            if (port != null) {
                url_bak = url_bak + host + ":" + port;
            } else {
                url_bak = url_bak + host;
            }

            if (splitTemp[1].contains("/")) {
                String[] urlTemp2 = splitTemp[1].split("/");
                if (urlTemp2.length > 1) {
                    for (int i = 1; i < urlTemp2.length; i++) {
                        url_bak = url_bak + "/" + urlTemp2[i];
                    }
                }
                System.out.println("url_bak:" + url_bak);
            } else {
                System.out.println("url_bak:" + url_bak);
            }
        }
        return url_bak;
    }


    /**
     * 将字符内容写入新创建的文件中
     * 先删掉原文件 再重新写入内容
     */

    private static final String FILE_NAME = "generalCompileSetting.gradle";

    public static void writerFileSting(VirtualFile path, String content) {
        try {
            File file = new File(path.getPath());//定义一个file对象，用来初始化FileReader
            if (file.exists()) {
                boolean delete = file.delete();
//                System.out.println(" file delete result " + delete);

            }
            if (!file.exists()) {
                boolean newFile = file.createNewFile();
                System.out.println(" file create result " + newFile);
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8));
                bufferedWriter.write(content);
                bufferedWriter.close();
            }
            System.out.println("file finish");
        } catch (IOException error) {
            System.out.println(" plugin message error " + error.getMessage());
        }
    }

    /**
     * 将文件中的内容按照过滤条件读取出来
     */
    public static String readFilterFileSting(VirtualFile path) throws IOException {
        File file = new File(path.getPath());//定义一个file对象，用来初始化FileReader
        FileReader reader = new FileReader(file);//定义一个fileReader对象，用来初始化BufferedReader
        BufferedReader bReader = new BufferedReader(reader);//new一个BufferedReader对象，将文件内容读取到缓存
        StringBuilder sb = new StringBuilder();//定义一个字符串缓存，将字符串存放缓存中
        String s = "";
        while ((s = bReader.readLine()) != null) {//逐行读取文件内容，不读取换行符和末尾的空格
            boolean isComment = false;
            String replace = s.trim();
            String trim = removeAllSpace(replace);
            // 简单的注解
            if (isSimpleComment(trim) || trim.length() == 0) {
                continue;
            }
            // 多行注解
            if (trim.startsWith("/*")) {
                isComment = true;
            }
            if (trim.startsWith("*")) {
                isComment = true;
            }
            if (isComment && trim.endsWith("*/")) {
                continue;
            }
            if (!isComment && isThirdAarLib(trim)) {
                sb.append(trim).append("\n");//将读取的字符串添加换行符后累加存放在缓存中
            }
        }
        bReader.close();
        return sb.toString();
    }

    private static final String TRIM_KEY_BEGIN = "generalCompileEnv_ThirdAarLib";
    private static final String TRIM_KEY_END = "]";

    /**
     * 如果是generalCompileEnv_ThirdAarLib字段，获取到该字段，并且拼接成字符串
     */

    private static boolean sIsThirdAarLib = false;

    private static boolean isThirdAarLib(String line) {
        //如果这一行是以 “generalCompileEnv_ThirdAarLib” 字段开头的
        if (line.startsWith(TRIM_KEY_BEGIN)) {
            sIsThirdAarLib = true;
            return true;
        }

        if (sIsThirdAarLib) {
            if (line.endsWith(TRIM_KEY_END)) {
                sIsThirdAarLib = false;
            }
            return true;
        }
        return false;
    }

    /**
     * 注意: 这里移除所有的空格,如果只想移除前后两端的,请使用;
     * *
     * * @param line
     * *
     * 行
     * * @return String
     */
    private static String removeAllSpace(String line) {
        StringBuilder b = new StringBuilder(line.length());
        for (char ch : line.toCharArray()) {
            if ((int) ch != 9 && (int) ch != 32) {
                b.append(ch);
            }
        }
        return b.toString();
    }

    /**
     * 移除单行注释
     */
    private static boolean isSimpleComment(String line) {
        return line.startsWith("//");
    }

    public static void centerDialog(ColumnDialog dialog, int width, int height) {
        dialog.setPreferredSize(new Dimension(width, height));
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //获取屏幕的尺寸
        int screenWidth = screenSize.width; //获取屏幕的宽
        int screenHeight = screenSize.height; //获取屏幕的高
        dialog.setLocation(screenWidth / 2 - width / 2, screenHeight / 2 - height / 2);//设置窗口居中显示
        dialog.setVisible(true);
    }

    public static void centerConfigDialog(ConfigDialog dialog, int width, int height) {
        dialog.setPreferredSize(new Dimension(width, height));
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //获取屏幕的尺寸
        int screenWidth = screenSize.width; //获取屏幕的宽
        int screenHeight = screenSize.height; //获取屏幕的高
        dialog.setLocation(screenWidth / 2 - width / 2, screenHeight / 2 - height / 2);//设置窗口居中显示
        dialog.setVisible(true);
    }

}
